<template>
  <ea-dialog
    v-bind="$attrs"
    v-on="$listeners"
    width="400px"
    ref="elDialog"
    :title="title"
    :type="type"
    @submit="submit"
    @reset="resetClear"
    @closed="handleClosed"
  >
    <el-form ref="form" :rules="rules" :model="singleData" label-width="100px">
      <el-form-item
        :label="(singleData.level === 1 ? '学校' : '学院') + '名称'"
        prop="orgaName"
      >
        <el-input v-model="singleData.orgaName"></el-input>
      </el-form-item>
      <el-form-item label="所属区域" prop="area" v-if="singleData.level === 1">
        <SelectCity
          @proClick="proClick"
          @cityClick="cityClick"
          :Province="singleData.provinceId"
          :cityCode="singleData.cityId"
        ></SelectCity>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input type="textarea" v-model="singleData.desc"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">{{
        type === "add" ? "添加" : "保存"
      }}</el-button>
      <el-button @click="resetClear()">重置</el-button>
    </span>
  </ea-dialog>
</template>

<script>
import editAndAdd from "@/mixins/business/editAndAdd";
import SelectCity from "@/views/component/selectCity";

export default {
  name: "EaOrg",
  mixins: [editAndAdd],
  components: { SelectCity },
  props: {
    dialogvisible: Boolean
  },
  data() {
    return {
      singleData: {
        desc: "",
        level: 1,
        orgaCode: "",
        orgaName: "",
        pcode: 0,
        provinceId: "", //省份ID
        cityId: "" //市ID
      },
      ProvChose: "",
      cityChose: "",
      rules: {
        orgaName: { required: true, message: "请输入学校名称！" }
      }
    };
  },
  computed: {
    title() {
      return (
        this.operationName[this.type] +
        (this.singleData.level === 1 ? "学校" : "学院")
      );
    }
  },
  watch: {
    singleData() {
      this.cityChose = [];
      this.cityChose.push(this.singleData.provinceId, this.singleData.cityId);
    },
    dialogvisible: {
      handler(n) {
        this.cityChose = [];
        this.singleData.provinceId = "";
        this.singleData.cityId = "";
      }
    }
  },
  methods: {
    handle() {
      if (this.type === "add") this.singleData.orgaCode = null;
      return new Promise((resolve, reject) => {
        this.axios
          .post("sys/sysOrganize/addOrUpdateOrga", this.singleData)
          .then(res => {
            if (this.type === "add" && this.singleData.level === 2)
              this.$refs["form"].resetFields();
            resolve(res);
          })
          .catch(reject);
      });
    },
    resetClear() {
      this.cityChose = [];
      this.singleData.provinceId = "";
      this.singleData.cityId = "";
      this.$refs["form"].resetFields();
    },
    handleClosed() {
      this.cityChose = [];
      this.singleData.pcode = 0;
      this.singleData.level = 1;
      this.end();
    },
    proClick(e) {
      //从城市组件接收到的数据
      this.singleData.provinceId = e;
    },
    cityClick(e) {
      //从城市组件接收到的数据
      this.singleData.cityId = e;
    }
  }
};
</script>

<style scoped></style>
